# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Contributor: David Macek <david.macek.0@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com>
# Contributor: Alethea Rose <alethea@alethearose.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=nodejs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=18.17.1
pkgrel=1
pkgdesc="An open-source, cross-platform JavaScript runtime environment (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://nodejs.org/"
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-nasm"
             "${MINGW_PACKAGE_PREFIX}-autotools")
depends=("${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "winpty")
source=("https://nodejs.org/dist/v${pkgver}/node-v${pkgver}.tar.xz"
        0002-Fix-system-icu-build.patch
        0004-Define-localtime_s-for-MinGW.patch
        0005-Remove-.lib-suffix-on-linked-libraries.patch
        0006-Define-_WIN32_WINNT-in-node.gypi.patch
        0007-Skip-sys-resource-and-unneeded-includes.patch
        0008-Link-with-municode-and-extern-wmain-for-Unicode.patch
        0010-Fix-incorrect-test-assumptions-for-MinGW.patch
        0011-Properly-detect-architecture.patch
        0012-Use-shell-wrapper-script-for-npm-on-MSYS2-MinGW-w64.patch
        0013-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch
        0016-Do-not-rely-on-__STDC_FORMAT_MACROS.patch
        0017-Do-not-get-confused-by-the-presence-of-winpthread.patch
        0021-Use-wide-character-Win32-API-functions-to-handle-wch.patch
        0022-Disable-MSVC_HACK.patch
        0023-Make-sure-that-__rdtsc-is-declared.patch
        0024-Fix-gyp-mingw.patch
        0025-Fix-globalPrefix-on-mingw.patch
        0026-Embed-manifest-in-node-exe.patch
        0101-node-buildflags-fixes.patch
        0102-set-cxx-standard-to-gnu.patch
        0103-node-gyp-support-mingw-toolchain.patch
        0104-v8-fix-static-assert-implementations.patch
        0105-v8-fix-conflicting-functions.patch
        0106-v8-support-clang-in-mingw-mode.patch
        0107-v8-disable-system-instrumentation.patch
        0108-v8-buildflags-fixes.patch
        0109-v8-builtin-deps-buildflags-fixes.patch
        node)

prepare() {
  cd "${srcdir}/node-v${pkgver}"

  patch -Np1 -i "${srcdir}/0002-Fix-system-icu-build.patch"
  patch -Np1 -i "${srcdir}/0004-Define-localtime_s-for-MinGW.patch"
  patch -Np1 -i "${srcdir}/0005-Remove-.lib-suffix-on-linked-libraries.patch"
  patch -Np1 -i "${srcdir}/0006-Define-_WIN32_WINNT-in-node.gypi.patch"
  patch -Np1 -i "${srcdir}/0007-Skip-sys-resource-and-unneeded-includes.patch"
  patch -Np1 -i "${srcdir}/0008-Link-with-municode-and-extern-wmain-for-Unicode.patch"
  patch -Np1 -i "${srcdir}/0010-Fix-incorrect-test-assumptions-for-MinGW.patch"
  patch -Np1 -i "${srcdir}/0011-Properly-detect-architecture.patch"
  patch -Np1 -i "${srcdir}/0012-Use-shell-wrapper-script-for-npm-on-MSYS2-MinGW-w64.patch"
  patch -Np1 -i "${srcdir}/0013-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch"
  patch -Np1 -i "${srcdir}/0016-Do-not-rely-on-__STDC_FORMAT_MACROS.patch"
  patch -Np1 -i "${srcdir}/0017-Do-not-get-confused-by-the-presence-of-winpthread.patch"
  patch -Np1 -i "${srcdir}/0021-Use-wide-character-Win32-API-functions-to-handle-wch.patch"
  patch -Np1 -i "${srcdir}/0022-Disable-MSVC_HACK.patch"
  patch -Np1 -i "${srcdir}/0023-Make-sure-that-__rdtsc-is-declared.patch"
  patch -Np1 -i "${srcdir}/0024-Fix-gyp-mingw.patch"
  patch -Np1 -i "${srcdir}/0025-Fix-globalPrefix-on-mingw.patch"
  patch -Np1 -i "${srcdir}/0026-Embed-manifest-in-node-exe.patch"

  patch -Np1 -i "${srcdir}/0101-node-buildflags-fixes.patch"
  patch -Np1 -i "${srcdir}/0102-set-cxx-standard-to-gnu.patch"
  patch -Np1 -i "${srcdir}/0103-node-gyp-support-mingw-toolchain.patch"
  patch -Np1 -i "${srcdir}/0104-v8-fix-static-assert-implementations.patch"
  patch -Np1 -i "${srcdir}/0105-v8-fix-conflicting-functions.patch"
  patch -Np1 -i "${srcdir}/0106-v8-support-clang-in-mingw-mode.patch"
  patch -Np1 -i "${srcdir}/0107-v8-disable-system-instrumentation.patch"
  patch -Np1 -i "${srcdir}/0108-v8-buildflags-fixes.patch"
  patch -Np1 -i "${srcdir}/0109-v8-builtin-deps-buildflags-fixes.patch"
}

build() {
  cd "${srcdir}/node-v${pkgver}"

  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=( --debug )
  fi

  local opt_cpu="x64"
  if [[ ${CARCH} == aarch64 ]]; then
    opt_cpu="arm64"
  elif [[ ${CARCH} == i686 ]]; then
    opt_cpu="x86"
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ./configure \
    --prefix="${MINGW_PREFIX}" \
    --dest-os=win \
    --dest-cpu=${opt_cpu} \
    --shared \
    --shared-cares \
    --shared-libuv \
    --shared-openssl \
    --shared-zlib \
    --with-intl=system-icu \
    --without-dtrace \
    --without-etw \
    "${extra_config[@]}"

  ${MINGW_PREFIX}/bin/python tools/gyp_node.py -f make
  make

  # flatten the nested node_modules/ hierarchy
  test -z "$(find deps/npm/node_modules | grep '^.\{140,\}$')" ||
  (cd deps/npm &&
   cp -R . ../npm.bup &&
   rm -rf node_modules &&
   ../../out/Release/node.exe ../npm.bup/bin/npm-cli.js install --production)
}

check() {
  cd "${srcdir}/node-v${pkgver}"

  # A few tests still fail due to a bug with pausing/resuming file streams
  ${MINGW_PREFIX}/bin/python tools/test.py --mode=release -J \
    cctest doctool inspector message parallel sequential tick-processor || true
}

package() {
  cd "${srcdir}/node-v${pkgver}"

  ${MINGW_PREFIX}/bin/python tools/install.py install "${pkgdir}" "/${MINGW_PREFIX}"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/nodejs"
  cp -r doc/api/{*.html,assets} "${pkgdir}${MINGW_PREFIX}/share/doc/nodejs"

  mv -v "${pkgdir}${MINGW_PREFIX}/bin/node"{.exe,_exe}
  install -Dm755 "${srcdir}/node" "${pkgdir}${MINGW_PREFIX}/bin/node"
  mv -v "${pkgdir}${MINGW_PREFIX}/bin/node"{_exe,.exe}

  # this is a bit redundant but the python install script only installs the posix npm / npx and corepack wrappers.
  # im not fluent enough in python to change this so atm this will have to do (ps. do not use install it leaves the older scripts if upgrading).

  # install the node-gyp wrappers
  cp -f "${srcdir}/node-v${pkgver}/deps/npm/bin/node-gyp-bin/node-gyp"{,.cmd} "${pkgdir}${MINGW_PREFIX}/bin"
  # install the corepack wrappers
  cp -f "${srcdir}/node-v${pkgver}/deps/npm/bin/corepack"{,.cmd} "${pkgdir}${MINGW_PREFIX}/bin"
  # install the npm / npx wrappers
  cp -f "${srcdir}/node-v${pkgver}/deps/npm/bin/npm"{,.cmd} "${pkgdir}${MINGW_PREFIX}/bin"
  cp -f "${srcdir}/node-v${pkgver}/deps/npm/bin/npx"{,.cmd} "${pkgdir}${MINGW_PREFIX}/bin"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/nodejs/LICENSE"
}

sha256sums=('f215cf03d0f00f07ac0b674c6819f804c1542e16f152da04980022aeccf5e65a'
            'a63d7153f3344e34da0370a03ecaf58a6995dbbcc1fc26030769335ff899d8c5'
            '9bd93dcabcb4db8154e0a92caab0bb7374a9cf40e05d74759a640e556ea82e25'
            'e1b32d777c8dfa0a658791107a9f2abbb1a12c760b18a0304eab16815dce640f'
            'e1870a2759d0ebcc1d195c78afb78a6a37ba65e3ff2010641b0a16ce508152d6'
            'e4723c014b72b032e57e68173f0f8238cfca466de5ac794432ddd2e041c82252'
            '2668e88bf28531e6b59fd9908abf70001539db71f72d80cbe4bd2c889948294c'
            '332046b3e136079c0b169aafa5877413c1c2fa4810bf1b3dac2d90a4fbeab9f4'
            '7d470c21c447437a64156b4fdbdfe71b96c14269e8e93112dabd9253ab0eb542'
            'b6f2044f415cad379159e820ea75fef8cdc3f49d88c5b07b8e18e9dd8e127051'
            '71105e468e9aedfd3498e0136c3b29f481345bf961fe73cb7cec5f5b5c02c298'
            '6feb843551412eb3baab1eae6e63d61518d5112c7b8966355cea70a210e5217a'
            '8221cc1a10b09d0b42216b86612c6efb69bc54cde5b23bb286c679a6d7ba32d0'
            '7cc9794f2305306883d678036c9ad3cb03430c4193be4ad0ec658d57b41b916c'
            '208e9816d851245271a78516e86505c03f3fc7918663372974a128e2f1f09c62'
            '936b01d17b5f3d856ee040a88238ccc8937666aefa593205182cad8bc7a26178'
            '7bee8d2586536388755826786311a475f2f1e4979c4a4723e1e98d125eb92eae'
            '545c206f7410df97f4a3ec87428c807ba2e3580092d8d38b1058569e1ae39f86'
            '966e86202e8b8efe1f0256dad6a619a8e3d5cdeeab8d23736ad30ebf01d68aad'
            'e1784ae7459290fb98d5b0594a778faa2bbeb7c7e8f98d4a572bb66021e304bd'
            'f1f0f90b93b9ca19457ac2639e224ce6f1c5dd04b5ada16b2c1694e89e7706a8'
            '199c3f1f030d87fe1b8fe9f6e4a3fcbdf48b49b2d38901a9fe9eb7d907f18895'
            'f945c699e82805cb0cf37898c9cc8b91f0702290700b2c8ecba1ba52a5c8eb9f'
            'ed8aa2622c47c4aee7e927f3b39ad424729fecf47fe50bfda974727e9c4f15a1'
            'f3ae2f1e0bf844d1131e195ad0aa266994271373795c83cca26b58a5e6d84ce7'
            'cef226046703de2170d2490d3473571b212db747e574111a27fa536f791a88f2'
            '1807c908fd57455b6a5ff3f98a69354cc55e69a9debb52a7d49c4a14aed3e358'
            '6df6d1a0a9ee09c896f6d7a9b5cbb93f9b538fbc493af1d0dc2d67ee6986f12b'
            '284251ff443506cd61530a026afb421d0084e4ad25c037bcfe3a045246d729a2')
